﻿$(function () {
    $('[data-toggle="tooltip"]').tooltip();
    $(".modal-link").click(function (event) {
        event.preventDefault();
        $('#myModal').removeData("modal");
        $('#myModal').modal();
        $('.alert.alert-danger').addClass('hidden');
        $("input[type=text], textarea").val("").removeClass('error');
        $('span.red-tooltip').remove();
        ShowSubmitButton(true);
    });

    jQuery.validator.addMethod("checkphone", function (phone_number, element) {
        return phone_number.length == 0 || phone_number.length > 9 && phone_number.match(/^\d{3} \d{3} \d{4}$|^\d{10}$/);
    }, "Please specify a valid Phone Number");

    jQuery.validator.addMethod("checkname", function (your_name, element) {
        return your_name.length == 0 || your_name.match(/(^[a-zA-Z]+$)|(^[a-zA-Z]+[ ][a-zA-Z]+$)/);
    }, "Please specify a valid Your Name");

    function SubmitForm(btn) {
        var btnId = btn.attr('id');
        if (btnId == 'btnSubmit') { ShowSubmitButton(false); }
        $.ajax({
            type: "POST",
            url: "home/addrecord",
            data: $('form.refer-friend').serialize(),
            success: function (result) {
                if (result.Success) {
                    $("#myModal").removeClass("fade").modal("hide");
                    $("#messages").addClass("fade").modal("show");
                }
                else {
                    $('.alert.alert-danger').removeClass('hidden');
                    var strError = 'Have an error occurred when trying to saving data.';
                    if (result.Message != null && result.Message.length > 0) {
                        strError = result.Message;
                    }
                    else if (result.FieldErrors != null) {
                        strError = '';
                        for (var i = 0; i < result.FieldErrors.length; i++) {
                            strError += '<div>';
                            strError = strError + 'Field name: ' + result.FieldErrors[i].FieldName;
                            for (var j = 0; j < result.FieldErrors[i].Errors.length; j++) {
                                if (j == 0) strError += ' - Errors: '
                                else strError += '; ';
                                strError += result.FieldErrors[i].Errors[j];
                            }
                            strError += '</div>';
                        }
                    }
                    $('.alert.alert-danger').html(strError);
                }
                if (btnId == 'btnSubmit') { ShowSubmitButton(true); }
            },
            error: function (err) {
                $('.alert.alert-danger').removeClass('hidden');
                $('.alert.alert-danger').html('Have an error occurred when trying to saving data. ' + err);
                if (btnId == 'btnSubmit') { ShowSubmitButton(true); }
            }
        });
    }
    function ShowSubmitButton(isShow) {
        if (isShow) {
            $('#btnSubmit').show();
            $('#icon-loading').hide();
        }
        else {
            $('#btnSubmit').hide();
            $('#icon-loading').show();
        }
    }
    $("#btnCancel").click(function () {
        SubmitForm($(this));
    });

    $("form.refer-friend").validate({
        submitHandler: function (form) {
            SubmitForm($('#btnSubmit'));
        },
        onfocusout: function (element) {
            if (!this.checkable(element)) {
                this.element(element);
            }
        },
        rules: {
            YourEmail: {
                required: true,
                email: true
            },
            FriendEmail: {
                required: true,
                email: true
            },
            YourName: {
                checkname: true
            },
            PhoneNumber: {
                checkphone: true
            }
        },
        errorPlacement: function (error, element) {
            var next = element.next();
            if (next != null) next.remove();
            var tooltip = $('<span data-toggle="tooltip" data-placement="top" title="' + error.text() + '" class="red-tooltip"><img src="/images/icon-error.gif" class="image-icon" /></span>');
            tooltip.insertAfter(element);
            $('[data-toggle="tooltip"]').tooltip();
        },
        success: function (success, element) {
            var next = element.nextSibling;
            if (next != null) next.remove();
            var spansuccess = $('<span class="red-tooltip"><img src="/images/icon-success.gif" class="image-icon" /></span>');
            spansuccess.insertAfter(element);
        },
        messages: {
            YourEmail: {
                required: "You must type your Email to continue",
                email: "You must type the valid Email to continue"
            },
            FriendEmail: {
                required: "You must type friend's Email to continue",
                email: "You must type the valid Email to continue"
            },
            YourName: {
                checkname: "You must type the valid name to continue"
            },
            PhoneNumber: {
                checkphone: "You must type the valid phone number to continue"
            }
        }
    });
}); 